﻿using System;
//using System.Drawing;
//using System.Runtime.InteropServices;
//using System.Windows.Forms;

namespace ConsoleExt
{

    public class ColorText
    {
        private ColorText() { }

        public ConsoleColor FgColor { get; set; }

        public ConsoleColor BgColor { get; set; }

        public object ObjText { get; set; }


        public ColorText(ConsoleColor fgColor, object objText, ConsoleColor bgColor)
        {
            this.FgColor = fgColor;
            this.ObjText = objText;
            this.BgColor = bgColor;
        }

        public override string ToString()
        {
            var oldFgColor = Console.ForegroundColor;
            var oldBgColor = Console.BackgroundColor;

            Console.ForegroundColor = this.FgColor;
            Console.BackgroundColor = this.BgColor;

            Console.Write(this.ObjText);

            Console.ForegroundColor = oldFgColor;
            Console.BackgroundColor = oldBgColor;

            return string.Empty;
        }
    }
    public static class SharpConsoleExtension
    {
        static SharpConsoleExtension()
        {
            //SetWindowPositionCenter();
        }

        #region 设置控制台窗体居中
        //private struct RECT { public int left, top, right, bottom; }

        //[DllImport("kernel32.dll", SetLastError = true)]
        //private static extern IntPtr GetConsoleWindow();
        //[DllImport("user32.dll", SetLastError = true)]
        //private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
        //[DllImport("user32.dll", SetLastError = true)]
        //private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);

        //private static IntPtr hWin = GetConsoleWindow();
        ///// <summary>
        ///// 控制台窗体居中
        ///// </summary>
        //private static void SetWindowPositionCenter()
        //{
        //    RECT rc;
        //    GetWindowRect(hWin, out rc);
        //    Screen scr = Screen.FromPoint(new Point(rc.left, rc.top));
        //    int x = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2;
        //    int y = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2;
        //    MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, true);
        //}
        #endregion


        /// <summary>
        /// 前景色：黑色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns></returns>
        public static string Black(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Black, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：藏蓝色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkBlue(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkBlue, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：深绿色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkGreen(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkGreen, objText, bgColor).ToString();
        }
        /// <summary>
        /// 前景色：深紫色（深蓝绿色）
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkCyan(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkCyan, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：深红色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkRed(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkRed, objText, bgColor).ToString();
        }
        /// <summary>
        /// 前景色：深紫红色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkMagenta(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkMagenta, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：深黄色（赭色）
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkYellow(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkYellow, objText, bgColor).ToString();
        }
        /// <summary>
        /// 前景色：灰色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Gray(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Gray, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：深灰色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string DarkGray(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.DarkGray, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：蓝色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Blue(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Blue, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：绿色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Green(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Green, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：青色（蓝绿色）
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Cyan(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Cyan, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：红色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Red(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Red, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：紫红色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Magenta(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Magenta, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：黄色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string Yellow(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.Yellow, objText, bgColor).ToString();
        }

        /// <summary>
        /// 前景色：白色
        /// </summary>
        /// <param name="objText"></param>
        /// <param name="bgColor">背景色</param>
        /// <returns>ColorText</returns>
        public static string White(this object objText, ConsoleColor bgColor = ConsoleColor.Black)
        {
            return new ColorText(ConsoleColor.White, objText, bgColor).ToString();
        }

        /// <summary>
        /// 在打印的文本前面 打印的文本
        /// </summary>
        /// <param name="objText">当前文本</param>
        /// <param name="preObjText">当前文本前面的文本</param>
        /// <returns></returns>
        public static string PreWrite(this object objText, object preObjText)
        {
            return preObjText.ToString() + objText.ToString();
        }

        /// <summary>
        /// 在打印的文本前面 打印的文本
        /// </summary>
        /// <param name="objText">当前文本</param>
        /// <param name="preObjText">当前文本前面的文本</param>
        /// <param name="fgColor">当前文本前面的文本的前景色</param>
        /// <returns></returns>
        public static string PreWrite(this object objText, object preObjText, ConsoleColor fgColor)
        {
            new ColorText(fgColor, preObjText, ConsoleColor.Black).ToString();
            return objText.ToString();
        }

        /// <summary>
        /// 控制台换行
        /// </summary>
        public static void WriteLine(this object objText)
        {
            Console.WriteLine(objText);
        }
    }
}